package cn.atzxc.qy.controller;

import cn.atzxc.qy.domain.ResponseResult;
import cn.atzxc.qy.domain.vo.StudentCategoryHelpVo;
import cn.atzxc.qy.service.StudentCategoryHelpVoService;
import cn.atzxc.qy.utils.JwtUtil;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/getStudentHelp")
public class StudentHelpController {
    @Autowired
    private StudentCategoryHelpVoService studentCategoryHelpVoService;


    //获取所有数
    @GetMapping("/list")
    public ResponseResult getCategoryHelper(){
        List<StudentCategoryHelpVo> studentHelpVoList = studentCategoryHelpVoService.getStudentCategoryHelpVoList();
        System.out.println("studenthelp:"+studentHelpVoList);
        for (StudentCategoryHelpVo studentCategoryHelpVo : studentHelpVoList) {
            String imageURL = studentCategoryHelpVo.getImageURL();
            String[] imageUrls = JSON.parseObject(imageURL, String[].class);
            studentCategoryHelpVo.setImageURL(imageUrls[0]);
        }
        return new ResponseResult(200,"获取成功",studentHelpVoList);
    }

    //通过id获取指定的数据
    @GetMapping("/getStudentHelpById")
    public ResponseResult getStudentHelpById(@RequestParam("id") Long id){

        //TODO 准备通过前端传递的token获取到当前登录的用户ID
        // 1.如果token为空，则说明用户没有登录，所以可以直接执行查看详情操作
        // 2.如果token不为空，则说明用户已经登录，需要从token中获取到userid判断是否与查询出来的所属用户的ID相同
        //      - 如果相同，则说明此条任务为该用户发布的 - 需要跳转到自己的界面，可以设置一个管理功能
        //      - 如果不同，则说明此条任务不是该用户发布的 - 正常显示

        System.out.println("接收到的ID为："+id);
        StudentCategoryHelpVo studentCategoryHelpVo = studentCategoryHelpVoService.selectOneStudentHelpVoById(id);
        System.out.println("获取到的数据为："+studentCategoryHelpVo);
        if(studentCategoryHelpVo == null){
            return new ResponseResult(400,"ID为空，请设置后在查询");
        }
        return new ResponseResult(200,"获取成功",studentCategoryHelpVo);
    }

    /**
     * 用户端插入一个校园互助的条目
     *      1.通过token获取到用户的userid
     *      2.封装实体类
     *      3.传递到service
     *
     * @param token
     * @param studenthelp
     * @return
     */
    @PostMapping("/insert")
    public ResponseResult insertStudentHelpVo(@RequestHeader("token") String token,
                                              @RequestHeader("isPublish") Boolean isPublish,
                                              @RequestBody StudentCategoryHelpVo studenthelp) throws Exception {
        System.out.println("获取到了"+studenthelp);

        studenthelp.getDescription();
        int num = studentCategoryHelpVoService.addStudentHelp(token,isPublish,studenthelp);

        return new ResponseResult(200,"获取成功");
    }
    //1.查询指定userId的数据
    //2.查询出指定任务类型的数据
    //3.查询出指定任务状态的数据
    //4.修改指定userid的指定shelp_id的数据
    //5.删除指定userid的指定shelp_id的数据

    //根据传递进来的ID删除指定数据
    @PostMapping("/delete")
    public ResponseResult deleteStudent(@RequestHeader("token") String token,
                                              @RequestBody String  shelp_id) throws Exception {
        System.out.println("传递的互助ID"+shelp_id);
        int num = studentCategoryHelpVoService.deleteStudentHelpVo(token,shelp_id);
        if(num > 0){
            return new ResponseResult(200,"删除成功");
        }
        return new ResponseResult(400,"删除失败");
    }
    //获取我的所有数据
    @GetMapping("/mylist")
    public ResponseResult getMyCategoryHelper(@RequestHeader("token") String token) throws Exception {
        List<StudentCategoryHelpVo> studentHelpVoList = studentCategoryHelpVoService.getMyStudentCategoryHelpVoList(token);
        System.out.println("studenthelp:"+studentHelpVoList);
        return new ResponseResult(200,"获取成功",studentHelpVoList);
    }



    @GetMapping("isMySelf")
    public ResponseResult isMySelf(@RequestHeader("token") String token,
                                   @RequestParam("id") Long id) throws Exception {
        Claims claims = JwtUtil.parseJWT(token);
        String subject = claims.getSubject();
        Long userId = Long.parseLong(subject);//封装成为一个long类型
        System.out.println("解析出的ID为："+userId);

        //获取指定任务的userid
        Long userid = studentCategoryHelpVoService.getUserIdBySHelpId(id);


        //如果相等返回400
        if(userId.equals(userid)){
            return new ResponseResult(201,"当前任务就是该商品发布");
        }else {
            return new ResponseResult(200,"当前任务不是该商品发布");
        }
    }




}
